import { toast, promptBox } from 'nc-lightapp-front';
import { getQueryCondition } from 'src/hrpub/common/utils/utils';

import deepCopy from 'src/hrpub/common/utils/deep-copy';

let currentOptSelectedRows = null;

export default class TableAction {
	constructor(comp) {
		this.comp = comp;
	}

	// 获取主表数据
	getMainTableData = async ({ conditions, pageInfo, pk_org, oid } = {}) => {
		const { props } = this.comp;
		const { emp, meta, dispatch, editTable } = props;

		// 保证调用方式有两种，传参，如果没有传惨就从store拿
		!conditions && (conditions = getQueryCondition(props, 'query'));
		!pageInfo && (pageInfo = emp.pageInfo);
		!pk_org && (pk_org = emp.orgValue ? emp.orgValue.refpk : '');
		!oid && (oid = meta.getMeta()['query'].oid);
		let postData = {
			"querytype": "tree",
			"querycondition": {
				"logic":"and",
				"conditions":[]
			},
			...conditions,
			pageInfo,
			pk_org,
			oid
		};
		editTable.setTableData('list', { rows: [] }, false);
		dispatch({
			type: 'emp/getMainTableData',
			payload: {
				postData: postData
			}
		}).then((res) => {
			this.successFun(res);
		});
	};

	successFun = (res) => {
		if (!res.success || !res.data || !res.data.data || !res.data.data.list) {
			return;
		}
		const { props } = this.comp;
		const { dispatch, editTable,emp} = props;
		let pageInfo = deepCopy(res.data.data.list.pageInfo);
		dispatch({
			type: 'emp/update',
			payload: {
				pageInfo: pageInfo
			}
		});
		editTable.setTableData('list', res.data.data.list, false);
		if(emp.widget){
			editTable.hideColByKey('list', 'operation')
		}else{
			editTable.showColByKey('list', 'operation')
		}
	};

	//获取表格高度
	getTableHeight = () => {
		const { props } = this.comp;
		const { emp } = props;
		const mainTable = document.getElementsByClassName('material-table');
		let tableHeight = 0;
		if (mainTable[0]) {
			if (emp.addPageStatus == 'edit') {
				tableHeight = mainTable[0].clientHeight - 300;
			} else {
				tableHeight = mainTable[0].clientHeight - 36;
			}
		}
		return tableHeight;
	};

	getMainTableHeight = () => {
		return window.innerHeight - 54 - 36 - 16 - 38 + 'px';
	};

	// 当用户点击了全部选择按钮
	selectAll = (props, tableId, status) => {
		this.comp.props.dispatch({
			type: 'emp/update',
			payload: {
				tableSelectAll: status
			}
		});
	};

	adjustDate = (record) => {
		const { props, action } = this.comp;
		const { dispatch } = props;
		let selectedRows = [ record ];
		dispatch({
			type: 'emp/update',
			payload: {
				beginDate: selectedRows[0].values['pk_psnjob.begindate'].value,
				beginDateOri: selectedRows[0].values['pk_psnjob.begindate'].value //原计划入职时间
			}
		});

		action.dateAct.openDateModal(selectedRows);
	};

	handleEntry = (record) => {
		const { action } = this.comp;
		let selectedRows = [ record ];
		action.formAct.toEditPage(selectedRows);
	};

	refuseEntry = (record) => {
		let selectedRows = [ record ];
		this.openReEntryModal(selectedRows);
	};

	sendMiss = (record) => {
		let selectedRows = [ record ];
		this.sendMissRemind(selectedRows);
	};

	confirRec = (record) => {
		let selectedRows = [ record ];
		this.confirmRec(selectedRows);
	};
	// 行上的操作按钮点击
	rowClickHandle = (key, record, index) => {
		const { props, action } = this.comp;
		const { dispatch } = props;
		let selectedRows = [ record ];
		// 入职办理
		if (key === 'handle') {
			if (selectedRows[0].values['workflow_state'].value == 7) {
				action.formAct.toEditPage(selectedRows);
			}
		} else if (key === 'chgbegindate') {
			//调整时间
			if (selectedRows[0].values['workflow_state'].value == 7) {
				dispatch({
					type: 'emp/update',
					payload: {
						beginDate: selectedRows[0].values['pk_psnjob.begindate'].value,
						beginDateOri: selectedRows[0].values['pk_psnjob.begindate'].value //原计划入职时间
					}
				});

				action.dateAct.openDateModal(selectedRows);
			}
		} else if (key === 'reject') {
			if (selectedRows[0].values['workflow_state'].value == 7) {
				//状态为待入职时
				//拒绝入职
				//this.rejectEntry(selectedRows);
				//alert('拒绝入职方法被注释了')
				this.openReEntryModal(selectedRows);
			}
		} else if (key === 'missremind') {
			if (selectedRows[0].values['workflow_state'].value == 102) {
				//状态为已入职时
				this.sendMissRemind(selectedRows);
			}
		} else if (key === 'confreceive') {
			if (selectedRows[0].values['workflow_state'].value == 102) {
				//状态为已入职时
				this.confirmRec(selectedRows);
			}
		}
	};

	// 主页面主表格行双击事件
	doubleClick = (record, index, fProps, e) => {
		const { props, action } = this.comp;
		const { dispatch,emp } = props;
		let selectedRows = [ record ];
		let workflow_state = record.values['workflow_state'].value;
			dispatch({
				type: 'emp/update',
				payload: {
					workflow_state: workflow_state
				}
			});
			if (workflow_state === '7') {
				//待入职
				action.formAct.toEditPage(selectedRows);
			} else if (workflow_state === '8' || workflow_state === '102') {
				//拒绝入职=8 , 已入职=102
				action.formAct.toBrowsePage(selectedRows);
			}
		
	};

	// 点击拒绝入职后打开打开弹窗
	openReEntryModal = async (selectedRows) => {
		const { props } = this.comp;
		const { emp } = props;
		currentOptSelectedRows = selectedRows;

		promptBox({
			color: 'info',
			title: emp.language['gx6008-000018'] /* 国际化处理： 提示*/,
			content: emp.language['gx6008-000214'], //是否拒绝入职
			beSureBtnClick: () => {
				this.rejectEntry();
			}
		});
	};

	// 关闭拒绝入职弹窗
	closeReEntryModal = () => {
		const { props } = this.comp;
		const { dispatch } = props;
		currentOptSelectedRows = null;

		dispatch({
			type: 'emp/update',
			payload: {
				reEntryModalVisible: false
			}
		});
	};

	//拒绝入职
	rejectEntry = async (selectedRows) => {
		const { props } = this.comp;
		const { dispatch, emp } = props;

		if (!selectedRows && currentOptSelectedRows) {
			selectedRows = currentOptSelectedRows;
		}

		let postData = {
			billid: selectedRows[0].values['pk_entryapply'].value,
			pk_org: emp.orgValue.refpk
		};

		try {
			let res = await dispatch({
				type: 'emp/rejectEntry',
				payload: {
					postData: postData
				}
			});

			if (res.success) {
				toast({
					color: 'success',
					content: emp.language['gx6008-000052'] //"操作成功"
				});

				dispatch({
					type: 'emp/update',
					payload: {
						reEntryModalVisible: false
					}
				});
				this.getMainTableData();
			}
		} catch (e) {
			return false;
		}

		return true;
	};

	//发送缺件提醒
	sendMissRemind = async (selectedRows) => {
		const { props } = this.comp;
		const { dispatch, emp } = props;

		let postData = {
			billid: selectedRows[0].values['pk_entryapply'].value,
			pk_org: emp.orgValue.refpk
		};
		try {
			let res = await dispatch({
				type: 'emp/sendMissRemind',
				payload: {
					postData: postData
				}
			});

			if (res.success) {
				toast({
					color: 'success',
					content: emp.language['gx6008-000052'] //"操作成功"
				});
			}
		} catch (e) {
			return false;
		}

		return true;
	};

	//确认收件
	confirmRec = async (selectedRows) => {
		const { props } = this.comp;
		const { dispatch, emp } = props;

		let postData = {
			billid: selectedRows[0].values['pk_entryapply'].value,
			pk_org: emp.orgValue.refpk
		};

		try {
			let res = await dispatch({
				type: 'emp/confirmRec',
				payload: {
					postData: postData
				}
			});

			if (res.success) {
				toast({
					color: 'success',
					content: emp.language['gx6008-000052'] //"操作成功"
				});
			}
		} catch (e) {
			return false;
		}

		return true;
	};
	//添加小部件逻辑
	getWidgetTableData = async ({value,pageInfo} = {}) => {
		const { props } = this.comp;
		const { emp, meta, dispatch, editTable } = props;

		// 保证调用方式有两种，传参，如果没有传惨就从store拿
		!pageInfo && (pageInfo = emp.pageInfo);
		!value && (value = emp.widgetValue)
		let todayFlag = null
		
		let postData = {}
		if(value == 'total'){
			postData = {
				pageInfo: {
					pageSize: parseInt(pageInfo.pageSize),
					total: parseInt(pageInfo.total),
					totalPage:parseInt(pageInfo.totalPage),
					pageIndex:  parseInt(pageInfo.pageIndex),
				},
				widgetCode:"6008C0D0",
				appCode:"60092020",
				pageCode:"60092020nccloud",
				areaCode:"list",
			};
		}else{
			value == 'todayAttend'?(todayFlag=true):(todayFlag=false)
			postData = {
				pageInfo: {
					pageSize: parseInt(pageInfo.pageSize),
					total: parseInt(pageInfo.total),
					totalPage:parseInt(pageInfo.totalPage),
					pageIndex: parseInt(pageInfo.pageIndex),
				},
				widgetCode:"6008C040",
				appCode:"60092020",
				pageCode:"60092020nccloud",
				areaCode:"list",
				todayFlag,
			};
		}
		console.log(postData);
		
		editTable.setTableData('list', { rows: [] }, false);
		dispatch({
			type: 'emp/getWidgetTableData',
			payload: {
				postData: postData
			}
		}).then((res) => {
			this.successFun(res);
		});
	};
}
